﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace University.Workshop.WinApp.HospitalizationFolder
{
    public partial class HospitalizationManagementForm : Form
    {
        private int hospitalizationId;
        private Model.Patient patient;
        private Model.Hospitalization hospitalization;
        private List<Model.HospitalizationMedicine> medicinesHospitalization;
        private List<Model.HospitalizationMedicalSupply> medicalSuppliesHospitalization;
        private List<Model.Medicine> medicines;
        private List<Model.MedicalSupplies> medicalSupplies;

        public HospitalizationManagementForm(int hospitalizationId)
        {
            InitializeComponent();
            this.hospitalizationId = hospitalizationId;
            hospitalization = DataAccess.HospitalizationDataAccess.ObtainHospitalization(hospitalizationId);
            patient = DataAccess.PatientDataAccess.ShowPatient(hospitalization.PatientId);
            medicinesHospitalization = (List<Model.HospitalizationMedicine>)DataAccess.HospitalizationDataAccess.ObtainMedicinesFromHospitalizationId(hospitalizationId);
            medicalSuppliesHospitalization = (List<Model.HospitalizationMedicalSupply>) DataAccess.HospitalizationDataAccess.GetMedicalSuppliesFromHospitalizationId(hospitalizationId);
            medicines = (List<Model.Medicine>)DataAccess.MedicineDataAccess.ObtainAllMedicines();
            medicalSupplies = (List<Model.MedicalSupplies>)DataAccess.MedicalSuppliesDataAccess.ObtainAllMedicalSupplies();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void HospitalizationManagementForm_Load(object sender, EventArgs e)
        {
            LoadHospitalizationData();
            LoadCustomData();
        }

        private void LoadCustomData()
        {
            List<string> medicinesStrings = new List<string>();
            foreach (var medicine in medicines)
            {
                medicinesStrings.Add(medicine.BrandName);
            }

            List<string> medicalSuppilesStrings = new List<string>();
            foreach (var medicine in medicalSupplies)
            {
                medicinesStrings.Add(medicine.ItemName);
            }

            cmBxMedicalSupply.DataSource = medicalSuppilesStrings;
            cmBxMedicines.DataSource = medicinesStrings;
        }

        private void LoadHospitalizationData()
        {
            txBxPatientId.Text = patient.PatientId;
            txbxPatientName.Text = patient.FirstName + " " + patient.FatherLastName;
            txBxBed.Text = hospitalization.BedId;
            medicinesGridView.DataSource = null;
            MedicalSuppliesGridView.DataSource = null;
            medicinesGridView.DataSource = medicinesHospitalization;
            MedicalSuppliesGridView.DataSource = medicalSuppliesHospitalization;

        }

        private void btnAddMedicine_Click(object sender, EventArgs e)
        {
            Model.HospitalizationMedicine medicine = new Model.HospitalizationMedicine() {
                    HospitalizationId = this.hospitalizationId,
                    Dosage = cmBxDosage.Text,
                    HospitalizationMedicineId= (medicinesHospitalization.Count+1),
                    MedicineId = cmBxMedicines.Text,
                    Price = 5,
                    Quantity = int.Parse(txBxMedicineQuantity.Text)
            };
            AddMedicineToList(medicine);
        }
        
        private void AddMedicineToList(Model.HospitalizationMedicine medicine)
        {
            bool verify = true;
            foreach (var med in medicinesHospitalization)
            {
                if (med.MedicineId == medicine.MedicineId)
                {
                    med.Quantity += medicine.Quantity;
                    verify = false;
                }
            }
            if (verify)
            {
                medicinesHospitalization.Add(medicine);
            }
            LoadHospitalizationData();
        }


        private void btnSaveClinicalReport_Click(object sender, EventArgs e)
        {
            AddClinicalReport();
        }

        private void AddClinicalReport()
        {
            Model.HospitalizationClinicalReport clinical = new Model.HospitalizationClinicalReport() { Date = DateTime.Today.ToShortDateString(), Description = txBxClinicalReport.Text,HospitalizationId = hospitalizationId};
            DataAccess.HospitalizationDataAccess.AddClinicalReport(clinical);
            txBxClinicalReport.Text = "";
            MessageBox.Show("Sus Cambios fueron guardados");
        }

        private void btnSaveChanges_Click(object sender, EventArgs e)
        {
            DataAccess.HospitalizationDataAccess.ModifyHospitalizationData(medicalSuppliesHospitalization);
            MessageBox.Show("Sus Cambios fueron guardados");
        }

        private void MedicalSuppliesGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void btnAddMedicalSupply_Click(object sender, EventArgs e)
        {
            Model.HospitalizationMedicalSupply medical = new Model.HospitalizationMedicalSupply()
            {
                HospitalizationId = this.hospitalizationId,
                Price = 5,
                HospitalizationMedicalSupplyId = medicalSuppliesHospitalization.Count+1,
                MedicalSupplyId = cmBxMedicalSupply.Text,
                Quantity = int.Parse(txBxMedicalSupplyQuantity.Text)
            };
            AddMedicalSupplyToList(medical);
        }

        private void AddMedicalSupplyToList(Model.HospitalizationMedicalSupply medicalSupply)
        {
            bool verify = true;
            foreach (var med in medicalSuppliesHospitalization)
            {
                if (med.MedicalSupplyId== medicalSupply.MedicalSupplyId)
                {
                    med.Quantity += medicalSupply.Quantity;
                    verify = false;
                }
            }
            if (verify)
            {
                medicalSuppliesHospitalization.Add(medicalSupply);
            }
            LoadHospitalizationData();
        }
    }
}
